/****************************************************************************
 * Copyright (C) 2012 Dj_Skual
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************/
#include "ListInfosWindow.h"
#include "../../FileOperations/fileops.h"
#include "../../Themes/CTheme.h"
#include "../../Saves/SaveTools.h"
#include "../../Saves/DataBin/WiiSaveCrypted.h"
#include "../../Gui/gui_banner.h"

InfosWindow::InfosWindow(Browser * b, int w, int h)
	: GuiFrame(w, h)
{
	browser = b;
	browser->IndexChanged.connect(this, &InfosWindow::OnBrowserIndexChange);
	
	selIndex = -1;
	databinTid = 0;
	fileType = TYPE_UNKNOW;
	
	fileCfg = Resources::GetImageData("icon_brows_cfg.png");
	fileDefault = Resources::GetImageData("icon_brows_default.png");
	fileExe = Resources::GetImageData("icon_brows_exe.png");
	fileFolder = Resources::GetImageData("icon_brows_folder.png");
	fileFont = Resources::GetImageData("icon_brows_font.png");
	fileImg = Resources::GetImageData("icon_brows_img.png");
	fileLang = Resources::GetImageData("icon_brows_lang.png");
	fileMii = Resources::GetImageData("icon_brows_mii.png");
	fileSound = Resources::GetImageData("icon_brows_sound.png");
	fileTheme = Resources::GetImageData("icon_brows_theme.png");
	fileTxt = Resources::GetImageData("icon_brows_txt.png");
	fileXml = Resources::GetImageData("icon_brows_xml.png");
	
	type_Wii = Resources::GetImageData("icon_type_wii.png");
	type_Wiiware = Resources::GetImageData("icon_type_wiiware.png");
	type_Gamecube = Resources::GetImageData("icon_type_gamecube.png");
	type_VC_NES = Resources::GetImageData("icon_type_vc_nes.png");
	type_VC_SNES = Resources::GetImageData("icon_type_vc_snes.png");
	type_VC_N64 = Resources::GetImageData("icon_type_vc_n64.png");
	type_VC_SMS = Resources::GetImageData("icon_type_vc_sms.png");
	type_VC_MD = Resources::GetImageData("icon_type_vc_md.png");
	type_VC_PCE = Resources::GetImageData("icon_type_vc_pce.png");
	type_VC_Neogeo = Resources::GetImageData("icon_type_vc_neogeo.png");
	type_VC_Arcade = Resources::GetImageData("icon_type_vc_arcade.png");
	type_VC_C64 = Resources::GetImageData("icon_type_vc_c64.png");
	type_Channel = Resources::GetImageData("icon_type_channel.png");
	type_Unknow = Resources::GetImageData("icon_type_unknow.png");
	
	iconImg = NULL;
	typeImg = NULL;
	
	nameTxt = new GuiText((char*)NULL, thInt("16 - mainbrowser browser infos text size"), thColor("r=0 g=0 b=0 a=255 - mainbrowser entry text color"));
	nameTxt->SetAlignment(ALIGN_LEFT | ALIGN_TOP);
	nameTxt->SetMaxWidth(width, SCROLL_HORIZONTAL);
	
	typeTxt = new GuiText((char*)NULL, thInt("16 - mainbrowser browser infos text size"), thColor("r=0 g=0 b=0 a=255 - mainbrowser entry text color"));
	typeTxt->SetAlignment(ALIGN_LEFT | ALIGN_TOP);
	typeTxt->SetMaxWidth(width, DOTTED);
	
	sizeTxt = new GuiText((char*)NULL, thInt("16 - mainbrowser browser infos text size"), thColor("r=0 g=0 b=0 a=255 - mainbrowser entry text color"));
	sizeTxt->SetAlignment(ALIGN_LEFT | ALIGN_TOP);
	sizeTxt->SetMaxWidth(width, DOTTED);
	
	path = new GuiText(tr( "Path:" ), thInt("16 - mainbrowser browser infos text size"), thColor("r=0 g=0 b=0 a=255 - mainbrowser entry text color"));
	path->SetAlignment(ALIGN_LEFT | ALIGN_BOTTOM);
	path->SetPosition(0, -58);
	
	pathTxt = new GuiText((char*)NULL, thInt("16 - mainbrowser browser infos text size"), thColor("r=0 g=0 b=0 a=255 - mainbrowser entry text color"));
	pathTxt->SetAlignment(ALIGN_LEFT | ALIGN_BOTTOM);
	pathTxt->SetPosition(0, -40);
	pathTxt->SetLinesToDraw(3);
	pathTxt->SetMaxWidth(width, WRAP);
}

InfosWindow::~InfosWindow()
{
	RemoveAll();
	
	if(iconImg)
		delete iconImg;
	if(typeImg)
		delete typeImg;
	delete nameTxt;
	delete typeTxt;
	delete sizeTxt;
	delete path;
	delete pathTxt;
	
	Resources::Remove(fileCfg);
	Resources::Remove(fileDefault);
	Resources::Remove(fileExe);
	Resources::Remove(fileFolder);
	Resources::Remove(fileFont);
	Resources::Remove(fileImg);
	Resources::Remove(fileLang);
	Resources::Remove(fileMii);
	Resources::Remove(fileSound);
	Resources::Remove(fileTheme);
	Resources::Remove(fileTxt);
	Resources::Remove(fileXml);
	
	Resources::Remove(type_Wii);
	Resources::Remove(type_Wiiware);
	Resources::Remove(type_Gamecube);
	Resources::Remove(type_VC_NES);
	Resources::Remove(type_VC_SNES);
	Resources::Remove(type_VC_N64);
	Resources::Remove(type_VC_SMS);
	Resources::Remove(type_VC_MD);
	Resources::Remove(type_VC_PCE);
	Resources::Remove(type_VC_Neogeo);
	Resources::Remove(type_VC_Arcade);
	Resources::Remove(type_VC_C64);
	Resources::Remove(type_Channel);
	Resources::Remove(type_Unknow);
}

void InfosWindow::OnBrowserIndexChange()
{
	SetInfos();
}

void InfosWindow::SetInfos()
{
	RemoveAll();
	
	if(iconImg)
		delete iconImg;
	iconImg = NULL;
	
	if(typeImg)
		delete typeImg;
	typeImg = NULL;
	
	if(!browser->GetEntrieCount())
		return;
	
	selIndex = browser->GetSelIndex();
	
	dir = browser->IsDir(selIndex);
	save = browser->IsSave(selIndex);
	databin = browser->IsDataBin(selIndex);
	mii = browser->IsMii(selIndex);
	itemName = browser->GetItemName(selIndex);
	fileExt = "";
	
	if(save) GetSaveIcon();
	else if(databin) GetDataBinIcon();
	else GetIconFromExt();
	iconImg->SetPosition(0, (save || databin) ? 0 : 5);	
	if(!save && !databin) iconImg->SetScale(1.5);
	
	GetTypeIcon();
	
	int y = iconImg->GetHeight()+10;
	nameTxt->SetText(browser->GetItemDisplayName(selIndex));
	nameTxt->SetPosition(0, y);
	y += 30;
	
	GetTypeText();
	typeTxt->SetPosition(0, y);
	y += 22;
	
	GetSizeText();
	sizeTxt->SetPosition(0, y);
	
	pathTxt->SetText(browser->GetCurrentPath());
	
	Append(nameTxt);
	Append(iconImg);
	Append(typeTxt);
	Append(sizeTxt);
	Append(path);
	Append(pathTxt);
}

void InfosWindow::GetIconFromExt()
{
	if(dir)
	{
		iconImg = new GuiImage(fileFolder);
		return;
	}
	
	if (mii)
	{
		iconImg = new GuiImage(fileMii);
		return;
	}
	
	fileExt = itemName;
	int index = fileExt.find_last_of('.');
	if(index > 0)
		fileExt = fileExt.substr(index);
	
	if(!fileExt.compare(".cfg"))
	{
		iconImg = new GuiImage(fileCfg);
		return;
	}
	else if (!CompareHomebrew(fileExt.c_str()))
	{
		iconImg = new GuiImage(fileExe);
		return;
	}
	else if(!CompareFont(fileExt.c_str()))
	{
		iconImg = new GuiImage(fileFont);
		return;
	}
	else if (!CompareImage(fileExt.c_str()))
	{
		iconImg = new GuiImage(fileImg);
		return;
	}
	else if(!CompareLanguageFiles(fileExt.c_str()))
	{
		iconImg = new GuiImage(fileLang);
		return;
	}
	else if(!CompareAudio(fileExt.c_str()))
	{
		iconImg = new GuiImage(fileSound);
		return;
	}
	else if(!CompareThemeFiles(fileExt.c_str()))
	{
		iconImg = new GuiImage(fileTheme);
		return;
	}
	else if(!fileExt.compare(".log") ||
			!fileExt.compare(".txt"))
	{
		iconImg = new GuiImage(fileTxt);
		return;
	}
	else if(!fileExt.compare(".xml"))
	{
		iconImg = new GuiImage(fileXml);
		return;
	}
	
	iconImg = new GuiImage(fileDefault);
}

void InfosWindow::GetSaveIcon()
{
	ByteArray banner;
	std::string savepath(browser->GetCurrentSelectedFilepath());
	savepath += "/banner.bin";
	
	LoadFileToMem(savepath, banner);
	
	iconImg = new GuiSaveIcon(banner);
}

void InfosWindow::FoundSaveType()
{
	fileType = TYPE_UNKNOW;
	
	if(!save && !databin)
		return;
	
	u64 tid = 0;
	if(save)
		tid = SaveTools::StrToHex64(itemName.c_str());
	else
		tid = databinTid;
	if(!tid)
		return;
	
	std::string ID = SaveTools::titleText(TITLE_UPPER(tid), TITLE_LOWER(tid));
	if(ID.empty())
		return;
	
	std::string id(ID);
	std::string id2(ID);
	id.erase(1);
	id2.erase(2);
	
	if(!id.compare("R") || !id.compare("S") || !id.compare("D"))
		fileType = TYPE_WII;
	else if(!id.compare("W"))
		fileType = TYPE_WIIWARE;
	else if(!id.compare("G") || !id.compare("P") || !id.compare("D") || !id.compare("U"))
		fileType = TYPE_GAMECUBE;
	else if(!id.compare("F"))
		fileType = TYPE_VC_NES;
	else if(!id.compare("J"))
		fileType = TYPE_VC_SNES;
	else if(!id.compare("N"))
		fileType = TYPE_VC_N64;
	else if(!id.compare("L"))
		fileType = TYPE_VC_SMS;
	else if(!id.compare("M"))
		fileType = TYPE_VC_MD;
	else if(!id.compare("P"))
		fileType = TYPE_VC_PCE;
	else if(!id.compare("E"))
	{
		if(!id2.compare("EA") || !id2.compare("EB"))
			fileType = TYPE_VC_NEOGEO;
		else
			fileType = TYPE_VC_ARCADE;
	}
	else if(!id.compare("C"))
		fileType = TYPE_VC_C64;
	else if(!id.compare("H") || !id2.compare("JO"))
		fileType = TYPE_CHANNEL;
}

void InfosWindow::GetTypeIcon()
{
	if(!save && !databin)
		return;
	
	FoundSaveType();
	
	switch (fileType)
	{
		case TYPE_WII:
			typeImg = new GuiImage(type_Wii);
			break;
		case TYPE_WIIWARE:
			typeImg = new GuiImage(type_Wiiware);
			break;
		case TYPE_GAMECUBE:
			typeImg = new GuiImage(type_Gamecube);
			break;
		case TYPE_VC_NES:
			typeImg = new GuiImage(type_VC_NES);
			break;
		case TYPE_VC_SNES:
			typeImg = new GuiImage(type_VC_SNES);
			break;
		case TYPE_VC_N64:
			typeImg = new GuiImage(type_VC_N64);
			break;
		case TYPE_VC_SMS:
			typeImg = new GuiImage(type_VC_SMS);
			break;
		case TYPE_VC_MD:
			typeImg = new GuiImage(type_VC_MD);
			break;
		case TYPE_VC_PCE:
			typeImg = new GuiImage(type_VC_PCE);
			break;
		case TYPE_VC_NEOGEO:
			typeImg = new GuiImage(type_VC_Neogeo);
			break;
		case TYPE_VC_ARCADE:
			typeImg = new GuiImage(type_VC_Arcade);
			break;
		case TYPE_VC_C64:
			typeImg = new GuiImage(type_VC_C64);
			break;
		case TYPE_CHANNEL:
			typeImg = new GuiImage(type_Channel);
			break;
		case TYPE_UNKNOW:
		default:
			typeImg = new GuiImage(type_Unknow);
			break;
	}
	
	typeImg->SetAlignment(ALIGN_RIGHT | ALIGN_TOP);
	Append(typeImg);
}

void InfosWindow::GetTypeText()
{
	std::string type = tr( "Type:" );
	type += " ";
	
	if(dir)
	{
		type += tr( "Folder" );
	}
	else if(save)
	{
		type += tr( "SaveGame" );
	}
	else if(databin)
	{
		type += tr( "Data.bin" );
	}
	else if(mii)
	{
		type += tr( "Mii" );
	}
	else if (!fileExt.compare(".cfg"))
	{
		type += tr( "Config" );
	}
	else if(!CompareHomebrew(fileExt.c_str()))
	{
		type += tr( "Homebrew" );
	}
	else if(!CompareFont(fileExt.c_str()))
	{
		type += tr( "Font" );
	}
	else if(!CompareImage(fileExt.c_str()))
	{
		type += tr( "Picture" );
	}
	else if(!CompareLanguageFiles(fileExt.c_str()))
	{
		type += tr( "Language" );
	}
	else if(!CompareAudio(fileExt.c_str()))
	{
		type += tr( "Audio" );
	}
	else if(!CompareThemeFiles(fileExt.c_str()))
	{
		type += tr( "Theme" );
	}
	else if(!fileExt.compare(".log") ||
			!fileExt.compare(".txt"))
	{
		type += tr( "Text" );
	}
	else if(!fileExt.compare(".xml"))
	{
		type += tr( "XML" );
	}
	else
	{
		type += tr( "File" );
	}
	
	typeTxt->SetText(type);
}

void InfosWindow::GetSizeText()
{
	std::string size = tr( "Size:" );
	size += " ";
	if(!dir)
	{
		u64 foldersize = 0;
		u32 filenumber = 0;
		if(save)
			GetFolderSize(browser->GetCurrentSelectedFilepath(), &foldersize, &filenumber);
		else
			foldersize = FileSize(browser->GetCurrentSelectedFilepath());
		
		if (foldersize < MBSIZE)
			size += fmt("%0.2fKB", foldersize / KBSIZE);
		else if (foldersize > MBSIZE && foldersize < GBSIZE)
			size += fmt("%0.2fMB", foldersize / MBSIZE);
		else
			size += fmt("%0.2fGB", foldersize / GBSIZE);
	}
	else
		size += tr( "Unknown" );
	
	sizeTxt->SetText(size);
}

void InfosWindow::GetDataBinIcon()
{
	ByteArray banner = WiiSaveCrypted::GetBannerAndTid(browser->GetCurrentSelectedFilepath(), &databinTid);
	
	iconImg = new GuiSaveIcon(banner);
}
